package com.yuanqi.contoller;

import com.yuanqi.service.InventoryStatsService;
import com.yuanqi.vo.InventoryStatsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存统计控制器
 */
@RestController
@RequestMapping("/ware/inventory-stats")
@CrossOrigin(origins = "*")
public class InventoryStatsController {

    @Autowired
    private InventoryStatsService inventoryStatsService;

    /**
     * 获取库存统计列表
     */
    @GetMapping("/list")
    public Map<String, Object> getInventoryStatsList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Long warehouseId,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String skuName,
            @RequestParam(required = false) String stockStatus,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 构建查询参数
            Map<String, Object> params = new HashMap<>();
            params.put("page", page);
            params.put("size", size);
            params.put("warehouseId", warehouseId);
            params.put("categoryId", categoryId);
            params.put("skuName", skuName);
            params.put("stockStatus", stockStatus);
            params.put("sortField", sortField);
            params.put("sortOrder", sortOrder);
            
            // 获取库存统计列表
            List<InventoryStatsVO> inventoryList = inventoryStatsService.getInventoryStatsList(params);
            
            // 获取统计数据
            Map<String, Object> stats = inventoryStatsService.getInventoryStats(params);
            
            // 获取总数
            long total = inventoryStatsService.getInventoryStatsCount(params);
            
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", Map.of(
                "records", inventoryList,
                "total", total,
                "current", page,
                "size", size,
                "stats", stats
            ));
            
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("data", null);
        }
        
        return result;
    }

    /**
     * 导出库存统计报表
     */
    @PostMapping("/export")
    public void exportInventoryStats(
            @RequestBody(required = false) Map<String, Object> params,
            HttpServletResponse response) {
        
        try {
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", 
                "attachment; filename=inventory_stats_" + System.currentTimeMillis() + ".xlsx");
            
            // 导出数据
            inventoryStatsService.exportInventoryStats(params, response);
            
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            try {
                response.getWriter().write("导出失败: " + e.getMessage());
            } catch (Exception ex) {
                // 忽略异常
            }
        }
    }

    /**
     * 获取库存统计概览
     */
    @GetMapping("/overview")
    public Map<String, Object> getInventoryOverview(
            @RequestParam(required = false) Long warehouseId,
            @RequestParam(required = false) Long categoryId) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("warehouseId", warehouseId);
            params.put("categoryId", categoryId);
            
            Map<String, Object> overview = inventoryStatsService.getInventoryOverview(params);
            
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", overview);
            
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("data", null);
        }
        
        return result;
    }

    /**
     * 获取仓库库存价值分布数据
     */
    @GetMapping("/warehouse-value")
    public Map<String, Object> getWarehouseValueData(
            @RequestParam(required = false) Long warehouseId,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String skuName) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("warehouseId", warehouseId);
            params.put("categoryId", categoryId);
            params.put("skuName", skuName);
            
            List<Map<String, Object>> data = inventoryStatsService.getWarehouseValueData(params);
            
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", data);
            
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("data", null);
        }
        
        return result;
    }

    /**
     * 获取商品分类库存数量分布数据
     */
    @GetMapping("/category-quantity")
    public Map<String, Object> getCategoryQuantityData(
            @RequestParam(required = false) Long warehouseId,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String skuName) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("warehouseId", warehouseId);
            params.put("categoryId", categoryId);
            params.put("skuName", skuName);
            
            List<Map<String, Object>> data = inventoryStatsService.getCategoryQuantityData(params);
            
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", data);
            
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("data", null);
        }
        
        return result;
    }
}